package com.ph.util.dbbackup;

import java.util.ResourceBundle;

import com.ph.util.DateUtil;
import com.ph.util.PropertiesUtil;

public class DBBackUpUserData extends DBBackUpEXE {

	@Override
	public boolean exportDataBase() throws Throwable {
		boolean isSuccess = false;
		String sResult = "";

		try {
			// 执行命令
			System.out.println(getExecuteBackupCommand());
			sResult = execCommand(getExecuteBackupCommand());
			if (sResult.equals("")) {
				isSuccess = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			sResult = "";
		}
		return isSuccess;
	}

	@Override
	public String getExecuteBackupCommand() {
		StringBuilder sb = new StringBuilder();
		ResourceBundle rb = PropertiesUtil.getResourceBundle("dbbackup");

		String currentDate = DateUtil.getCurrentDateString();// 执行备份命令当前日期
		String sIp = rb.getString("dbserver");// IP
		String sDBInstallPath = rb.getString("dbinstallpath");// 数据库安装路径
		String sAccount = rb.getString("username");// 帐号
		String sPassword = rb.getString("password");// 密码
		String sDBName = rb.getString("dbname");// 备份数据库名
		String sBackupPath = rb.getString("dbbackuppath");// 备份路径
		String sBackupFileName = rb.getString("dbbackupfilename");// 备份数据库文件名
		String sBackupPurviewDataFileName = rb
				.getString("dbbackuppurviewdatafilename");
		String sBackupUserDataFileName = rb
				.getString("dbbackupadmindatafilename");

		String sAdminUserName = rb.getString("adminUserName");
		// cmd /c "%MYSQL_HOME%\bin\mysqldump.exe" --opt -h192.168.100.122 -uwjs
		// -p123456 -t phi p_base_purview_functiontype p_base_purview_function
		// --skip-lock-tables >d:\xxx.sql
		sb.append("cmd /c \"").append(sDBInstallPath).append(
				"bin\\mysqldump.exe\" ");
		sb.append("--opt -h").append(sIp);
		sb.append(" -u").append(sAccount);
		sb.append(" -p").append(sPassword);
		sb.append(" -t ").append(sDBName);
		sb.append(" p_base_purview_user --where=UserName='").append(
				sAdminUserName).append("' ");
		sb.append(" --skip-lock-tables");
		sb.append(" > ").append(sBackupPath).append(sBackupUserDataFileName);
		sb.append(currentDate).append(".sql");

		return sb.toString();
	}

	@Override
	public DBBackUpEXE createEXE() {
		return new DBBackUpUserData();
	}
}
